home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 316 / libsrc / atof.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  600 b   |  41 lines

  1.  
  2. /* jrd's kludgy atof */
  3.  
  4. #include <ctype.h>
  5.  
  6. double atof(str)
  7. char * str;
  8. {
  9.   int neg = 0;
  10.   int one = 1, ten = 10;
  11.   double accum, factor;
  12.   char c;
  13.  
  14.   while (isspace(*str)) str++;
  15.  
  16.   if (*str == '-') 
  17.     {
  18.     neg = 1;
  19.     str++;
  20.     }
  21.  
  22.   accum = 0;
  23.   for ( ; ((c = *str++) && (isdigit(c))) ; )
  24.     {
  25.     accum = accum * 10 + (c - '0');
  26.     }
  27.   if (c != '.')
  28.     return(accum);
  29.  
  30.   factor = (double )one / (double )ten;
  31.   for ( ; ((c = *str++) && (isdigit(c))) ; )
  32.     {
  33.     accum = accum + ((c - '0') * factor);
  34.     factor = factor / 10;
  35.     }
  36.  
  37.   if (neg)
  38.     accum = -accum;
  39.   return(accum);
  40. }
  41.